from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

font_path = '/System/Library/Fonts/STHeiti Light.ttc'
my_font = FontProperties(fname=font_path, size=12)
title_font = FontProperties(fname=font_path, size=16)

sns.set(style="whitegrid")

data = {
    'BMI_范围': [
        'BMI:20-30',
        'BMI:28-32',
        'BMI:27-35',
        'BMI:29-36',
        'BMI:31-46'
    ],
    't90_median': [
        4.4566,  
        11.2661,  
        18.2023,  
        24.2356,  
        34.6794   
    ]
}

# 使用字典创建DataFrame
summary = pd.DataFrame(data)


# --- 图 2：最终建议条形图 (显示中位数) ---
plt.figure(figsize=(14, 9))
barplot = sns.barplot(data=summary, x='BMI_范围', y='t90_median', palette='viridis')

# 在条形图上添加中位数作为数值标签
for index, row in summary.iterrows():
    barplot.text(index, row.t90_median + 0.5, f"{row.t90_median:.1f} 周",
                 color='black', ha="center", fontproperties=my_font)

plt.title('不同BMI范围孕妇的最佳NIPT检测时点建议', fontproperties=title_font)
plt.xlabel('孕妇BMI范围分组', fontproperties=my_font)
plt.ylabel('推荐孕周 t87.5 (中位数)', fontproperties=my_font)
plt.xticks(rotation=0, ha='center', fontproperties=my_font)

# 统一显示所有图表
plt.tight_layout()
plt.show()